var app = getApp()
Page({
    data: {
        show: false,
        imgList: [],
        openid: '',
        now_time: '',
        line1: '图文/介绍',
        active: '0',
        second_hand_detail: null,
        comment: {
            // _id:
            // _openid:
            text: '',
            avatarUrl: '',
            time:'',
            formId: "",
        },
        comments: [],
        getComments: 0
    },
    inputComment(e) {
        //console.log(e)
        this.setData({
            'comment.text': e.detail.value
        })
    },

    addFriend(e) {
        //console.log(e)
        if (e.detail.errMsg != "addFriend:ok") {
            qq.showToast({
                icon: 'none',
                title: '对方未开启添加，建议留言'
            })
        }
    },
    doComment(e) {
        //console.log(e)
          var time = new Date()
      var theTime = time.getFullYear() + '年' + (time.getMonth() + 1) + "月" + time.getDate() + "日 "
      if(time.getHours()<10){
        theTime+='0'
      }
      theTime+=time.getHours()+":"
      if(time.getMinutes()<10){
        theTime+='0'
      }
      theTime+=time.getMinutes()
        this.setData({
            'comment._openid': app.globalData.openid,
            'comment.avatarUrl': app.globalData.userInfo.avatarUrl,
            'comment.time': theTime,
            'comment.formId': e.detail.formId
        })
        if (this.data.comment.text) {
            this.updateDB()
        }
    },
    updateDB() {
        var that = this
        qq.request({
            url: 'https://api.weixin.qq.com/tcb/databaseupdate?access_token=' + app.globalData.access_token, // 仅为示例，并非真实的接口地址
            header: {
                'content-type': 'application/json' // 默认值
            }, method: "post",
            data: {
                "env": app.globalData.myEnv,
                "query": "db.collection(\"second_hand\").doc(\"" + that.data.second_hand_detail._id + "\").update({\
        data: {\
               comments: db.command.push(["+ JSON.stringify(that.data.comment) + "])\
               }\
         })"
            },
            success(res) {
                //console.log(res)
                qq.showToast({
                    title: "留言成功"
                })
                that.data.second_hand_detail.comments.push(that.data.comment)
                that.setData({
                    'second_hand_detail.comments':that.data.second_hand_detail.comments,
                    'comment.text':''
                })

            },
            fail() {
                qq.showToast({
                    icon: "none",
                    title: "请检查网络"
                })
            }
        })
    },
    onLoad(option) {
        this.setData({
            second_hand_detail: JSON.parse(option.item)
        })
        //console.log(JSON.parse(option.item))
    },
    onReady() {
        this.setData({
            openid: app.globalData.openid,
            show: true,
        })
    },
    tabChange(e) {
        this.setData({
            line1: e.currentTarget.dataset.text,
            active: e.currentTarget.id,

        })
    },
    ViewImage(e) {
        wx.previewImage({
            urls: this.data.imgList,
            current: e.currentTarget.dataset.url
        });
    }
})